<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
	<meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="format-detection" content="telephone=no,email=no">
    <meta name="screen-orientation" content="portrait">
    <meta name="x5-orientation" content="portrait">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-title" content="我的奖品">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <title>我的奖品</title>

    <!-- <link rel="stylesheet" type="text/css" href="https://cdn.bootcss.com/layer/3.0.1/mobile/need/layer.css?2.0"> -->
    <link rel="stylesheet" type="text/css" href="../css/base.css?v=20170912001">
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/layer/3.0.1/mobile/layer.min.js"></script>
    <script src="https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js"></script>
    <script src="../js/common.js?v=20170814001"></script>
    <script src="../js/conf.js?v=20170801001"></script>
    <script>
	   $(function(){
        FastClick.attach(document.body);
        var params = _CZWeb.getUrlParams();
        var brandid = params['brandid'];
        
        var loadingIndex = null, serverTime = null;

        

        var page = 1,size = 10,loading = false,scrollHeight = 0,over = false;

        var $loadMore = $('<div class="more">加载更多</div>');

        //显示套餐
        var showView = function(list){
            var itemsObject = $('.items');
            var flag = true;
            $.each(list, function(index, val) {
                if (val.type==-1) {//谢谢参与
                    return ;
                }
                var btnText = "点击使用",btnClass="grayscale",img=(val.gtype==2||val.gtype==5)?"../images/lottery_list_ll.png":(val.gtype==1?"../images/lottery_list_hf.png":val.img);
                switch(val.status)
                {
                    case 0: btnClass="";break;
                    case 1: btnText = "已使用";break;
                    case 2: btnText = "已失效";break;
                    case 3: btnText = "领取中";break;
                }

                var object = $('<div class="item"><div class="info flex flex-x-center flex-y-center '+btnClass+'"><div class="img" style="background-image:url('+img+')"></div><div class="name flex-1">'+this.goodsname+'</div><div class="button">'+btnText+'</div></div><div class="footer">'+('使用有效期：'+(this.ctime).substring(0,10)+'至'+(this.valid_date).substring(0,10)) +'</div></div>');

                object.data(val);
                itemsObject.append(object);
                flag = false;
            });
            if(flag){
                if(page==1)
                    itemsObject.before('<div class="nothing">暂时没有奖品哦</div>').remove();
                else{
                    $loadMore.addClass('hidden');
                }
            }else{
                $loadMore.appendTo(itemsObject);
                if(list.length<size){
                    $loadMore.addClass('hidden');
                }else{
                    $loadMore.removeClass('hidden');
                    page++;
                }
            }
        };

        

        var getList = function(page,size){
            //拉取信息
            $.ajax({url: _lotteryListAction,type: 'POST',dataType: 'json',data: $.extend({},{page:page,size:size},params),beforeSend:function(){
                loadingIndex = layer.loading();
            }})
            .done(function(data) {
                if(data&&data.result==0){
                    //显示套餐
                    showView(data.prize_logs);
                }else{
                    layer.msg((data&&data.reason)||'系统错误!');
                }
            })
            .fail(function() {
                layer.msg('发送请求失败!');
            })
            .always(function(){
                layer.close(loadingIndex);
                loadingIndex = null;
            });
        };


        getList(page,size);

        $('.page-lottery-list').on('click','.item .button',function(e){
            var $button = $(this),info=$button.parents('.info'),item = $button.parents('.item');
            if (info.hasClass('grayscale')) {
                return;
            }
            if(item.data('type')!=0){
                $.post(_lotteryApplyAction,$.extend({},params,{id:item.data('id')})
                ,function(data){
                    if(data&&data.result==0){
                        info.addClass('grayscale');
                        if(item.data('gtype')==2){//流量
                            $button.html('领取中');
                            location.href = _CZWeb.getAdspaceUrl(brandid,data.flow_url,'流量领取',false);
                        }if(item.data('gtype')==5){
                            location.href = _CZWeb.getAdspaceUrl(brandid,data.url,'流量卡领取',false);
                        }else{
                            $button.html('已使用');
                            layer.msg('使用成功!'+(item.data('message')?('<br>'+item.data('message')):''),5);
                        }
                    }else{
                        layer.msg((data&&data.reason)||'系统错误!');
                    }
                },'json');
            }else{
                layer.msg('暂不能填写收货地址!');
                //location.href = _CZWeb.getAdspaceUrl(brandid,_host+"/address.html?index="+item.data('index'),"收货地址",false);
            }
        }).on('click','.more',function(){
            getList(page,size);
        });
    });
    </script>
</head>
<body>
    <div class="page page-lottery-list">
        <div class="items"></div>
    </div>
    <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?9b40deeaa9ce094daadf13e0dc1a4729";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
    </script>
</body>
</html>